﻿using GadgetStore.Data.Models;
using GadgetStore.Web.Adapters;
using GadgetStore.Web.Adapters.Interfaces;
using GadgetStore.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace GadgetStore.Web.Controllers
{
    [Authorize(Roles="Admin")]
    public class ApiGadgetController : ApiController
    {
        private IGadget _adapter;
        public ApiGadgetController()
        {
            _adapter = new GadgetAdapter();
        }
        [AllowAnonymous]
        public IHttpActionResult GET(int id = -1)
        {
            if (id == -1)
            {
                List<Gadget> Gadgets = _adapter.GetAllGadgets();
                return Ok(Gadgets);
            }
            else
            {
                GadgetVM GadgetVM = _adapter.GetOneGadget(id);
                return Ok(GadgetVM);
            }
        }
        [HttpPost]
        public IHttpActionResult POST(Gadget gadget)
        {
            
            int NewGadgetId = _adapter.NewGadget(gadget);
            return Ok(NewGadgetId);
        }
        [HttpDelete]
        public IHttpActionResult DELETE(int id)
        {
            _adapter.DeleteGadget(id);
            return Ok();
        }
        [HttpPut]
        public IHttpActionResult PUT(Gadget gadget)
        {
            int GadgetId = _adapter.UpdateGadget(gadget);
            return Ok(GadgetId);
        }
    }
}
